<?php
class Application_Model_BlogMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Blog');
		}
		return $this->_dbTable;
	}

	public function save(Application_Model_Blog $blog)
	{
		$data = array(
            'id'        => $blog->getId(), 
			'title'     => $blog->getTitle(),
			'content'   => $blog->getContent(),
		);
		
		if (null === ($id = $blog->getId())) {
			unset($data['id']);
            $data['id'] = $this->getDbTable()->insert($data);
            //save keyword
            $blog->id = $data['id'];
            $keywordM = new Application_Model_KeywordMapper();
            $keywordM->save($blog);
		//save commit 
            $commit  = new Application_Model_Commit();
            $commitM = new Application_Model_CommitMapper();
            $commit  ->setBlogId($data['id'])
                     ->setParentId(0)
                     ->setfrom("Ghold")
                     ->setContent($data['title']);
            $data['commit_id'] = $commitM->save($commit);
            $this->getDbTable()->update($data, array('id = ?' => $data['id']));
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }
	
	public function find($id, Application_Model_Blog $blog)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row     = $result->current();
        $keyword = $row->findManyToManyRowset('Application_Model_DbTable_Keyword','Application_Model_DbTable_BlogKeyword');
        $blog ->setId($row->id)
              ->setTitle($row->title)
              ->setContent($row->content)
              ->setAcType($row->type)
              ->setAuthor($row->author)
              ->setCommitId($row->commit_id)
              ->setKeyword($keyword->toArray())
              ->setCreateDt($row->create_dt)
              ->setModifyDt($row->modify_dt);
	}

    public function fetchAll()
    {
        $resultBlog = $this->getDbTable()->fetchAll(null,'create_dt DESC');
        $entries   = array();
        foreach ($resultBlog as $row) {
            $resultKeyword = $row->findManyToManyRowset('Application_Model_DbTable_Keyword','Application_Model_DbTable_BlogKeyword');
            $entry = new Application_Model_Blog();
            $entry->setId($row->id)
                  ->setTitle($row->title)
                  ->setContent($row->content)
                  ->setAcType($row->type)
                  ->setAuthor($row->author)
                  ->setCommitId($row->commit_id)
                  ->setKeyword($resultKeyword->toArray())
                  ->setCreateDt($row->create_dt)
                  ->setModifyDt($row->modify_dt);
            $entries[] = $entry;
        }
        return $entries;
    }
}
